package com.emerquim.persistence.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.apache.log4j.Logger;

import com.emerquim.bean.dao.ConexionJDBC;





/**
 * PSubclaseOnuId entity. @author MyEclipse Persistence Tools
 */

public class PSubclaseOnuId  implements java.io.Serializable {

	/**
	 * Funcionalidad de log
	 */
	private static final Logger log = Logger.getLogger(PSubclaseOnuId.class);


	// Fields    

	private long sbonuId;
	private PClaseOnu PClaseOnu;


	// Constructors

	/** default constructor */
	public PSubclaseOnuId() {
	}


	/** full constructor */
	public PSubclaseOnuId(long sbonuId, PClaseOnu PClaseOnu) {
		this.sbonuId = sbonuId;
		this.PClaseOnu = PClaseOnu;
	}


	// Property accessors

	public long getSbonuId() {
		return this.sbonuId;
	}

	public void setSbonuId(long sbonuId) {
		this.sbonuId = sbonuId;
	}

	public PClaseOnu getPClaseOnu() {
		return this.PClaseOnu;
	}

	public void setPClaseOnu(PClaseOnu PClaseOnu) {
		this.PClaseOnu = PClaseOnu;
	}




	public boolean equals(Object other) {
		if ( (this == other ) ) return true;
		if ( (other == null ) ) return false;
		if ( !(other instanceof PSubclaseOnuId) ) return false;
		PSubclaseOnuId castOther = ( PSubclaseOnuId ) other; 

		return ( (this.getSbonuId()==castOther.getSbonuId()) || ( new Long(this.getSbonuId())!=null && new Long(castOther.getSbonuId())!=null && new Long(this.getSbonuId()).equals(new Long(castOther.getSbonuId())) ) )
		&& ( (this.getPClaseOnu()==castOther.getPClaseOnu()) || ( this.getPClaseOnu()!=null && castOther.getPClaseOnu()!=null && this.getPClaseOnu().equals(castOther.getPClaseOnu()) ) );
	}

	public int hashCode() {
		int result = 17;

		result = 37 * result + ( new Long(getSbonuId()) == null ? 0 : new Long(this.getSbonuId()).hashCode() );
		result = 37 * result + ( getPClaseOnu() == null ? 0 : this.getPClaseOnu().hashCode() );
		return result;
	}   

	/**
	 * Metodo para autogenerar un id para la tabla de sub clases onu
	 * @author Felipe Criales
	 */
	public void generarSbonuIdIncrement(){	   
		int idGenerado = 1;
		Connection conexion = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			conexion = ConexionJDBC.getSingleton().getConnection();		
			preparedStatement = conexion.prepareStatement("select max(sbonu_id) from p_subclase_onu");
			resultSet = preparedStatement.executeQuery();			
			while (resultSet.next()) {
				idGenerado = resultSet.getInt(1);
			}			
			idGenerado++;
		} catch (Exception e) {
			log.error("Error al generar un id para la tabla de clases onu");
		}
		finally {
			ConexionJDBC.close(resultSet);
			ConexionJDBC.close(preparedStatement);	
			ConexionJDBC.close(conexion);
		}		
		this.sbonuId = idGenerado;		
	}
}